package model;

import entity.Teacher;
import iface.IModel;
import java.text.SimpleDateFormat;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class TeacherTableModel extends AbstractTableModel implements IModel<Teacher> {

    private manager.Manager manager;

    public TeacherTableModel(manager.Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        int count = 0;
        for (int i = 0; i < this.manager.getTeacherManager().size(); i++) {
            if (this.manager.getTeacherManager().get(i).isStatus()) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Teacher teacher = this.manager.getTeacherManager().get(rowIndex);

        if (teacher.isStatus() == true) {
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            
            switch (columnIndex) {
                case 0:
                    return teacher.getTeacherID();
                case 1:
                    return teacher.getTeacherName();
                case 2:
                    return teacher.getSalaryScore();
                case 3:
                    return teacher.getSeniorityScore();
                case 4:
                    return teacher.getAllowance();
                case 5:
                    return df.format(teacher.getDateIn());
                default:
                    return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Mã Giáo Viên";
            case 1:
                return "Tên Giáo Viên";
            case 2:
                return "Hệ Số Lương Cơ Bản";
            case 3:
                return "Phụ Cấp Công Tác";
            case 4:
                return "Phụ Cấp Chức Vụ";
            case 5:
                return "Năm Vào Nghành";
            default:
                return null;
        }
    }

    @Override
    public void add(Teacher obj) {
        this.manager.getTeacherManager().add(obj);
        this.fireTableDataChanged();
    }

    @Override
    public void replace(int index, Teacher obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void remove(Teacher obj) {
        this.manager.getTeacherManager().remove(obj);
        this.fireTableDataChanged();
    }

    @Override
    public void refresh() {
        this.fireTableDataChanged();
    }

    @Override
    public Teacher get(int index) {
        int j = 0;
        for (int k = 0; k < this.manager.getTeacherManager().size(); k++) {
            if (this.manager.getTeacherManager().get(k).isStatus()) {
                if (index == j) {
                    return this.manager.getTeacherManager().get(k);
                }
                j++;
            }
        }
        return null;
    }
}
